﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WebSEClassLibrary
{
    /// <summary>
    /// 业务逻辑层的控制类
    /// </summary>
    public class Controller
    {
        /// <summary>
        /// 当前状态
        /// </summary>
        private State currentState;

        public State CurrentState
        {
            get { return currentState; }
            set { currentState = value; }
        }
        /// <summary>
        /// 当前房间信息
        /// </summary>
        private Room currentRoom;

        public Room CurrentRoom
        {
            get { return currentRoom; }
            set { currentRoom = value; }
        }
        #region 公用方法
        /// <summary>
        /// 构造函数
        /// </summary>
        public Controller()
        {
            initState(StateEnum.Begin);
        }
        /// <summary>
        /// 根据personId,返回下一个对话
        /// </summary>
        /// <param name="personId">在当前状态中,跟哪一个person谈话</param>
        /// <param name="choiceID">选项ID</param>
        /// <returns>若对话没结束,则返回下一个对话,否则,更新State,并返回null</returns>
        public Dialog getNextDialog(int personId,int choiceId)
        {
            Person tempPerson = ( Person )(this.currentState.PersonList[personId]);
            if (tempPerson.getNextDialog(choiceId) == true)
            {
                this.updateState(tempPerson.NextState);
                return null;
            }
            else
                return tempPerson.CurrentDialog;
        }
        /// <summary>
        /// 初始化某一个状态
        /// </summary>
        /// <param name="newState">新状态</param>
        /// <returns>若初始化成功,则返回true,否则返回false</returns>
        public bool initState(StateEnum newState)
        {
            currentRoom = new Room();
            currentState = new State(newState);
            return true;
            //throw new NotImplementedException();
            //return true;
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="newState">新状态</param>
        /// <returns>若更新成功,则返回true,否则返回false</returns>
        private bool updateState(StateEnum newState)
        {
            if (newState == this.currentState.StateID)
                return true;
            return this.initState(newState);
        }
        #endregion
    }
}
